package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IAlarmtypeService;
import com.ruoyi.system.service.IAppmanagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IQstagService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目点表配置Controller
 * 
 * @author yaozh
 * @date 2024-02-24
 */
@RestController
@RequestMapping("/system/qstag")
public class QstagController extends BaseController
{
    @Resource
    private IQstagService qstagService;
    @Resource
    private IAlarmtypeService alarmtypeService;
    @Resource
    private IAppmanagerService appManagerService;

    /**
     * 查询项目点表配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:qstag:list')")
    @GetMapping("/list")
    public TableDataInfo list(Qstag qstag,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Qstag> list = qstagService.selectQstagList(qstag,dbName);
        return getDataTable(list);
    }

    /**
     * 导出项目点表配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:qstag:export')")
    @Log(title = "项目点表配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Qstag qstag,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<Qstag> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = qstagService.selectQstagList(qstag,dbName);
        }
        ExcelUtil<Qstag> util = new ExcelUtil<Qstag>(Qstag.class);
        util.exportExcel(response, list, "项目点表配置数据");
    }

    @Log(title = "项目点表配置", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:qstag:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @RequestParam String dbName) throws Exception
    {
        ExcelUtil<Qstag> util = new ExcelUtil<Qstag>(Qstag.class);
        List<Qstag> qsTagList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = qstagService.importQsTag(qsTagList, updateSupport, operName, dbName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Qstag> util = new ExcelUtil<Qstag>(Qstag.class);
        util.importTemplateExcel(response, "项目点表配置数据");
    }

    /**
     * 获取项目点表配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:qstag:query')")
    @GetMapping(value = "/{tagid}")
    public AjaxResult getInfo(@PathVariable("tagid") Long tagid,@RequestParam String dbName)
    {
        return success(qstagService.selectQstagByTagid(tagid,dbName));
    }

    /**
     * 新增项目点表配置
     */
    @PreAuthorize("@ss.hasPermi('system:qstag:add')")
    @Log(title = "项目点表配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Qstag qstag,@RequestParam String dbName)
    {
        return toAjax(qstagService.insertQstag(qstag,dbName));
    }

    /**
     * 修改项目点表配置
     */
    @PreAuthorize("@ss.hasPermi('system:qstag:edit')")
    @Log(title = "项目点表配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Qstag qstag,@RequestParam String dbName)
    {
        return toAjax(qstagService.updateQstag(qstag,dbName));
    }

    /**
     * 删除项目点表配置
     */
    @PreAuthorize("@ss.hasPermi('system:qstag:remove')")
    @Log(title = "项目点表配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tagids}")
    public AjaxResult remove(@PathVariable Long[] tagids,@RequestParam String dbName)
    {
        return toAjax(qstagService.deleteQstagByTagids(tagids,dbName));
    }

    /**
     * 不分页，查询驱动列表
     */
    @PreAuthorize("@ss.hasPermi('system:qstag:list')")
    @GetMapping("/listAllAlarmType")
    public AjaxResult listAll(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(alarmtypeService.selectAlarmtypeForAll(dbName));
    }

}
